<?php
/*
Copyright 2008, Nathalia


Author:
Nathalia Sautchuk Patrício          (nathalia.sautchuk@gmail.com)

*/
class Editar extends TPage
{
	public function onInit($param)
	{
		parent::onInit($param);

        $this->congressoList->DataTextField='Nome';
		$this->congressoList->DataValueField='CongressoID';
		$this->congressoList->DataSource=CongressoRecord::finder()->findAll();
		$this->congressoList->dataBind();  

		if(!$this->IsPostBack)  // if the page is initially requested
		{
            $anaisRecord                = $this->AnaisRecord;
            $this->Nome->Text           = $anaisRecord->Nome;          
            $this->Editora->Text        = $anaisRecord->Editora;          
            $this->Status->Text         = $anaisRecord->Status;        
            $this->congressoList->setSelectedValue($anaisRecord->CongressoID);             
		}
	}

	public function editarAnais($sender,$param)
	{
		if($this->IsValid)  // when all validations succeed
		{
            $anaisRecord                 = $this->AnaisRecord;
            $anaisRecord->Nome           = $this->Nome->Text; 
            $anaisRecord->Editora        = $this->Editora->Text; 
            $anaisRecord->Status         = $this->Status->Text;   
            $anaisRecord->DataPublicacao = date('Y-m-d H-i-s');  
            $anaisRecord->CongressoID    = $this->congressoList->getSelectedValue();        

			// saves to the database via Active Record mechanism
			$anaisRecord->save();

			// redirects the browser to the homepage
			$this->Response->redirect('./index.php?page=Anais.Listar&all=1');
		}
	}

	protected function getAnaisRecord ()
	{
		// if the 'username' GET var is not empty and the current user
		// is an administrator, we use the GET var value instead.
		if($this->Request['anaisID'] !== null)
			$anaisID = $this->Request['anaisID'];

		// use Active Record to look for the specified username
		$anaisRecord= AnaisRecord::finder()->findByAnaisID($anaisID);
		if(!($anaisRecord instanceof AnaisRecord))
			throw new THttpException(500,'AnaisID is invalid.');
		return $anaisRecord;
	}


}
?>
